import QtQuick 1.0
import com.nokia.symbian 1.0
import QtMobility.connectivity 1.2
import Nfc 1.0

Page {
    id: page3
    anchors.fill: parent
    tools: toolBarNfc

    // Tag data list
    ListModel {
        id: tagListModel
    }

    // Use either NearField QML element or NDefManager. Do not use them at the same time.
    // Comment either element away.
    // NearField only reads tags
    // NdefManager is based on the QNearFieldManager

//    NearField {
//        // NFC RTD-URI type
//        // And minimum and maximum number of record the NDEF message must contain
//        filter: [ NdefFilter { type: "urn:nfc:wkt:U"; minimum: 1; maximum: 1 } ]

//        // indicates whether the order of records should be taken into account
//        // when matching messages
//        orderMatch: false

//        // Provides a list of NdefRecords
//        onMessageRecordsChanged: {
//            tagDetectedDialog.titleText = "Tag Detected";
//            tagDetectedDialog.open();
//            var recordLength = messageRecords.length;
//            for (var i = 0; i < recordLength; ++i) {
//                if (messageRecords[i].recordType === "urn:nfc:wkt:U") {
//                    tagListModel.append({"tagText": messageRecords[i].uri});
//                }
//            }
//        }
//    }


    NdefManager {
        id: ndefManager
        onNfcReadTagUri: {
            if (tab2content.status == PageStatus.Active) {
                tagListModel.append({"tagText": nfcTagUri});
            }
            tagDetectedDialog.titleText = "Tag Detected";
            tagDetectedDialog.open();
        }
        onNfcReadTagText: {
            if (tab2content.status == PageStatus.Active) {
                tagListModel.append({"tagText": nfcTagText});
            }
            tagDetectedDialog.titleText = "Tag Detected";
            tagDetectedDialog.open();
        }
        onNfcWriteResult: {
            if (result) {
                tagDetectedDialog.titleText = "Write Successful";
                tagDetectedDialog.open();
            }
            else {
                tagDetectedDialog.titleText = "Write Failed";
                tagDetectedDialog.open();
            }
        }
    }

    ToolBarLayout {
        id: toolBarNfc
        ToolButton {
            flat: true
            iconSource: "toolbar-back"
            onClicked: pageStack.pop()
        }
        TabButton {
            tab: tab1content
            text: "NFC \nDetect"
        }
        TabButton {
            tab: tab2content
            text: "NFC \nRead"
        }
        TabButton {
            tab: tab3content
            text: "NFC \nWrite"
        }
        ToolButton {
            flat: true
            iconSource: "toolbar-menu"
            onClicked: nfcOptionsMenu.open()
        }
    }

    TabGroup {
        id: tabGroup
        anchors {
            bottom: toolBarNfc.top
            left: parent.left
            right: parent.right
            top: parent.top
        }

        Page {
            id: tab1content
        }

        Page {
            id: tab2content
            anchors.fill: parent

            ListView {
                id: tagListView
                height: page3.height - parent.height
                width: parent.width
                model: tagListModel

                delegate: Item {
                    id: tagListItem
                    height: platformStyle.graphicSizeMedium
                    width: parent.width

                    Text {
                        id: tagText
                        text: model.tagText
                        font {
                            family: platformStyle.fontFamilyRegular
                            pixelSize: platformStyle.fontSizeLarge
                        }
                        color: platformStyle.colorNormalLight
                    }
                }
            }
            ScrollDecorator {
                flickableItem: tagListView
            }
        }

        Rectangle {
            id: tab3content
            anchors.fill: parent
            Column {
                anchors.fill: parent
                spacing: platformStyle.paddingLarge
                TextArea {
                    id: tagTextArea
                    width: parent.width
                    placeholderText: "Enter here data you want to write to the tag"
                }
                Button {
                    text: "Write URI into the tag"
                    width: parent.width
                    onClicked: ndefManager.nfcWriteUriTag(tagTextArea.text)
                }
                Button {
                    text: "Write text into the tag"
                    width: parent.width
                    onClicked: ndefManager.nfcWriteTextTag(tagTextArea.text)
                }
            }
        }
    }

    Menu {
        id: nfcOptionsMenu
        content: MenuLayout {
            MenuItem {
                text: "Dummy 1"
            }
            MenuItem {
                text: "Dummy 2"
            }
        }
    }

    CommonDialog {
        id: tagDetectedDialog
        titleText: "NFC Tag Detected"
        anchors {
            verticalCenter: parent.verticalCenter
            left: parent.left
            leftMargin: 10
        }
        height: parent.height / 2
        buttons: ToolBar {
            id: buttons
            width: parent.width
            tools: ToolButton {
                text: "Ok"
                width: parent.width
                onClicked: {
                    tagDetectedDialog.accept();
                }
            }
        }
        content: Image {
            id: nfcImage
            anchors.centerIn: parent
            source: "qrc:/images/nfc.png"
        }
    }
}
